package com.sinaapp.gavinzhang.GSpider.http;

import com.sinaapp.gavinzhang.GSpider.config.GSpiderConfig;
import com.sinaapp.gavinzhang.GSpider.config.PageProcessorConfig;
import org.apache.log4j.Logger;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import java.util.concurrent.ExecutorService;

/**
 * 用于Http展示用
 * 冗余代码太多，亟需整理
 * Created by gavin on 15-9-29.
 */
public class HttpServer implements Runnable{

    private static final Logger LOGGER = Logger.getLogger(HttpServer.class);


    private boolean shutdown = false;
    private ExecutorService executor;
    private GSpiderConfig gSpiderConfig;
    private PageProcessorConfig pageProcessorConfig;

    public HttpServer(ExecutorService executor, GSpiderConfig gSpiderConfig, PageProcessorConfig pageProcessorConfig) {
        this.executor = executor;
        this.gSpiderConfig = gSpiderConfig;
        this.pageProcessorConfig = pageProcessorConfig;
    }

    public HttpServer() {
    }

    public static void main(String[] args){
        HttpServer server = new HttpServer();
        server.await();
    }

    @Override
    public void run() {
        await();
    }

    public void await()
    {
        try {
            Server server = new Server(40000);

            ServletContextHandler context = new ServletContextHandler(
                    ServletContextHandler.SESSIONS);
            context.setContextPath("/");
            server.setHandler(context);

            context.addServlet(new ServletHolder(new DispatchServlet()),"/*");
            LOGGER.info("server start.");
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
